//The MIT License

//Copyright (c) 2009 Lluis Sanchez, Nick Darnell

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.Reflection;
using System.Reflection.Emit;
using Polymorph.Properties;

namespace Polymorph
{
    /// <summary>
    /// The <see cref="CodeEquals"/> class represents an == operator.
    /// </summary>
    public class CodeEquals : CodeConditionExpression
    {
        private CodeExpression m_exp1;
        private CodeExpression m_exp2;
        private Type m_t1;
        private Type m_t2;

        /// <summary>
        /// Initializes a new instance of <see cref="CodeEquals"/>.
        /// </summary>
        /// <param name="exp1">The first expression to compare.</param>
        /// <param name="exp2">The second expression to compare.</param>
        public CodeEquals(CodeExpression exp1, CodeExpression exp2)
        {
            m_exp1 = exp1;
            m_exp2 = exp2;

            m_t1 = exp1.ResultType;
            m_t2 = exp2.ResultType;

            if (m_t1.IsValueType && m_t2.IsValueType)
            {
                if (m_t1 != m_t2)
                {
                    throw new InvalidOperationException(
                        Resources.CanNotCompareDifferentPrimitiveTypes);
                }
            }
        }

        /// <summary>
        /// Generates the equal expression using the provided IL generator.
        /// </summary>
        /// <param name="gen">The IL generator.</param>
        public override void Generate(ILGenerator gen)
        {
            if (m_t1.IsPrimitive)
            {
                m_exp1.Generate(gen);
                m_exp2.Generate(gen);
                gen.Emit(OpCodes.Ceq);
            }
            else
            {
                m_exp1.Generate(gen);
                m_exp2.Generate(gen);
                gen.EmitCall(OpCodes.Callvirt, m_t1.GetMethod("Equals", new Type[] { m_t2 }), null);
            }
        }

        /// <summary>
        /// Generates a the equals expression as a branch operation.
        /// </summary>
        /// <param name="gen">The IL generator to use.</param>
        /// <param name="label">The label to jump to.</param>
        /// <param name="branchCase">The branch case we are jumping on.</param>
        public override void GenerateForBranch(ILGenerator gen, Label label, bool branchCase)
        {
            if (m_t1.IsPrimitive)
            {
                m_exp1.Generate(gen);
                m_exp2.Generate(gen);
                if (branchCase)
                    gen.Emit(OpCodes.Beq, label);
                else
                    gen.Emit(OpCodes.Bne_Un, label);
            }
            else
            {
                Generate(gen);
                if (branchCase)
                    gen.Emit(OpCodes.Brtrue, label);
                else
                    gen.Emit(OpCodes.Brfalse, label);
            }
        }

        /// <summary>
        /// Prints the code for the equals expression in a human readable format.
        /// </summary>
        /// <param name="cw">The code writer.</param>
        public override void PrintCode(CodeWriter cw)
        {
            m_exp1.PrintCode(cw);
            cw.Write(" == ");
            m_exp2.PrintCode(cw);
        }

        /// <summary>
        /// Gets the result of the equals expression.
        /// </summary>
        public override Type ResultType
        {
            get { return typeof(bool); }
        }
    }
}